package com.asd.map.service.impl;


import com.alibaba.fastjson.JSONArray;
import com.asd.common.util.OkHttp;
import com.asd.map.entity.Information;
import com.asd.map.entity.InformationToLink;
import com.asd.map.entity.Point_position;
import com.asd.map.entity.Point_style;
import com.asd.map.entity.vo.Point_positionVo;
import com.asd.map.mapper.Point_positiontableMapper;
import com.asd.map.mapper.Point_styleMapper;
import com.asd.map.service.IInformationService;
import com.asd.map.service.IPoint_positionService;
import com.asd.system.mapper.ImagesMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.*;

/**
 * @author
 * @createtime 2021-09-02 9:49
 * @description
 */
@Service
public class Point_positionServiceImpl extends ServiceImpl<Point_positiontableMapper, Point_position> implements IPoint_positionService {


    @Autowired
    private Point_positiontableMapper point_positiontableMapper;

    @Autowired
    private Point_styleMapper pointStyleMapper;

    @Autowired
    private ImagesMapper imagesMapper;
    @Autowired
    private IInformationService iInformationService;


    @Override
    public List<Point_position> getByDid(String id) {
        List<Point_position> pointList = point_positiontableMapper.getByDid(id);


        return pointList;
    }

    @Override
    public List<Point_positionVo> getByDid1(String did, String applyId) {
        Information information = new Information();
        information.setId(did);
        Information information1 = iInformationService.getById(information);
        String link = information1.getLink();
        //数据源类型
        String addtype = information1.getAddtype();
        String data = information1.getData();

        List<Point_positionVo> pointPositionVos = new ArrayList<>();
        //判断数据源添加类型
        if (addtype.equals("2")) {
            //获取接口数据，并进行解析
            try {
                String json = OkHttp.run(link);
                List<Map<String, String>> listJson = (List<Map<String, String>>) JSONArray.parse(json);


                if (listJson != null) {
                    //System.out.println(listJson);
                    for (int i = 0; i < listJson.size(); i++) {
                        Point_positionVo point_positionVo1 = new Point_positionVo();
                        Map<String, String> stringStringMap = listJson.get(i);
                        point_positionVo1.setId(stringStringMap.get("id"));
                        point_positionVo1.setDid(did);
                        point_positionVo1.setLng(stringStringMap.get("lng"));
                        point_positionVo1.setLat(stringStringMap.get("lat"));

//                        Object primaryEchoPathlist2 = stringStringMap.get("primaryEchopathlist");
//                        //将“[]"转换为[]
//                        String primaryEchoPathlist = String.valueOf(primaryEchoPathlist2);
//                        List<String> primaryEchoPathlist1 = new ArrayList<>();
//                        if (!primaryEchoPathlist.equals("[]") && primaryEchoPathlist != null) {
//                            String[] sourceStrArray = primaryEchoPathlist.split("\"");
//                            primaryEchoPathlist1.add(sourceStrArray[1]);
//                            point_positionVo1.setPrimaryEchoPathlist(primaryEchoPathlist1);
//                        }
//
//
//                        List viceEchoPathlist2 = Collections.singletonList(stringStringMap.get("viceEchopathlist"));
//                        if (viceEchoPathlist2 != null && viceEchoPathlist2.size() != 0) {
//                            point_positionVo1.setViceEchoPathlist((List<String>) viceEchoPathlist2.get(0));
//                        }
                        List<List<String>> dataJson = (List<List<String>>) JSONArray.parse(data);
                        if (dataJson != null) {
                            List<Map<String, String>> objects = new ArrayList<>();
                            for (int i1 = 0; i1 < dataJson.size(); i1++) {
                                Map<String, String> map1 = new HashMap<>();
                                //System.out.println(dataJson.get(i1));
                                String key = dataJson.get(i1).get(0);
                                //System.out.println("key===="+key);
                                String Ekey = dataJson.get(i1).get(1);
                                //System.out.println("Ekey===="+Ekey);
                                List data1 = Collections.singletonList(stringStringMap.get("data"));

                                List data2 = (List) data1.get(0);
                                //System.out.println(data2);
                                for (int i2 = 0; i2 < data2.size(); i2++) {
                                    List data3 = (List) data2.get(i2);
                                    String str = (String) data3.get(1);
                                    if (Ekey.equals(str) && Ekey.equals("name")) {
                                        map1.put("valueName", key);
                                        map1.put("name", (String) data3.get(0));
                                    }
                                    if (Ekey.equals(str) && !Ekey.equals("name")){
                                        map1.put("valueName", key);
                                        map1.put("value", (String) data3.get(0));
                                    }
                                }
                                Object isAdd1 = stringStringMap.get("isAdd");
                                String isAdd = String.valueOf(isAdd1);
                                String link1 = stringStringMap.get("link");
                                String button = stringStringMap.get("button");
                                if (isAdd.equals("true")) {
                                    map1.put("isAdd", "true");
                                    map1.put("link", link1);
                                    map1.put("button", button);
                                }
                                objects.add(map1);
                            }
                            point_positionVo1.setData(net.sf.json.JSONArray.fromObject(objects).toString());
                        }
                        List<Point_style> styleList = pointStyleMapper.findByApplyId(applyId, did);
                        for (Point_style pointStyle : styleList) {

                            if (pointStyle.getType().equals("0")) {
                                point_positionVo1.setPointStyle(pointStyle);
                            } else {
                                if (point_positionVo1.getId().equals(pointStyle.getPid())) {
                                    point_positionVo1.setPointStyle(pointStyle);
                                }
                            }

                        }

                        pointPositionVos.add(point_positionVo1);
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {

            List<Point_position> pointList = point_positiontableMapper.getByDid(did);

            List<Point_style> styleList = pointStyleMapper.findByApplyId(applyId, did);

//            List<Point_positionVo> pointPositionVos = new ArrayList<>();

            for (int i = 0; i < pointList.size(); i++) {

                Point_position point_position = pointList.get(i);
                String pid = point_position.getId();

                List<String> viceEchoPathlist = imagesMapper.findViceEchoPathByPid(pid);

                List<String> primaryEchoPathlist = imagesMapper.findprimaryEchoPathByPid(pid);

                Point_positionVo point_positionVo = new Point_positionVo();

                point_positionVo.setId(point_position.getId());
                point_positionVo.setDid(did);
                point_positionVo.setData(point_position.getData());
                point_positionVo.setLng(point_position.getLng());
                point_positionVo.setLat(point_position.getLat());
                point_positionVo.setPrimaryEchoPathlist(primaryEchoPathlist);
                point_positionVo.setViceEchoPathlist(viceEchoPathlist);

                for (Point_style pointStyle : styleList) {

                    if (pointStyle.getType().equals("0")) {
                        point_positionVo.setPointStyle(pointStyle);
                    } else {
                        if (point_position.getId().equals(pointStyle.getPid())) {
                            point_positionVo.setPointStyle(pointStyle);
                        }
                    }

                }

                pointPositionVos.add(point_positionVo);
            }
//            return pointPositionVos;
        }

        return pointPositionVos;
    }

    //测试
    public static void main(String[] args) {
        String sourceStr = "[https://blog.csdn.net/weixin_43786637/article/details/105231070]"; //,"https://blog.csdn.net/weixin_43786637/article/details/105231070","https://blog.csdn.net/weixin_43786637/article/details/105231070","https://blog.csdn.net/weixin_43786637/article/details/105231070","https://blog.csdn.net/weixin_43786637/article/details/105231070"]";
        ArrayList<String> objects = new ArrayList<>();
        String[] sourceStrArray = sourceStr.split(",");
//        System.out.println();
        if (sourceStrArray.length == 1) {

//            System.out.println(sourceStrArray[0]);
            objects.add(sourceStrArray[0]);
        } else {
            for (int i = 0; i < sourceStrArray.length; i++) {
                if (i == 0) {
                    String substring = sourceStrArray[i].substring(1);
                    objects.add(substring);
                } else if (i == sourceStrArray.length - 1) {
                    String[] split = sourceStrArray[i].split("]");
                    objects.add(split[0]);
                } else {
                    objects.add(sourceStrArray[i]);
                }
            }
        }

    }

    @Override
    public Point_positionVo findById(String id) {
        Point_position point_position = point_positiontableMapper.selectById(id);

        Point_positionVo point_positionVo = new Point_positionVo();

        List<String> viceEchoPathlist = imagesMapper.findViceEchoPathByPid(id);
//            String viceEchoPath = JSONArray.fromObject(viceEchoPathlist).toString();

        List<String> primaryEchoPathlist = imagesMapper.findprimaryEchoPathByPid(id);
//            String primaryEchoPath = JSONArray.fromObject(primaryEchoPathlist).toString();

        point_positionVo.setId(point_position.getId());
        point_positionVo.setDid(id);
        point_positionVo.setData(point_position.getData());
        point_positionVo.setLng(point_position.getLng());
        point_positionVo.setLat(point_position.getLat());
        point_positionVo.setPrimaryEchoPathlist(primaryEchoPathlist);
        point_positionVo.setViceEchoPathlist(viceEchoPathlist);

        return point_positionVo;
    }


    @Override
    public void insertBatch(List<Point_position> list) {

        for (int i = 0; i < list.size(); i++) {

            Point_position point_position = new Point_position();

            point_position = list.get(i);

            point_positiontableMapper.insert(point_position);

        }
    }

    @Override
    public List<Point_position> findByDid(Page<Point_position> positionPage, String did) {

        List<Point_position> list = point_positiontableMapper.findByDid(positionPage, did);
        return list;
    }

    @Override
    public List<Point_positionVo> findByDid1(Page<Point_positionVo> positionVoPage, String did) {

        List<Point_positionVo> positionList = point_positiontableMapper.findByDid1(positionVoPage, did);

        List<Point_positionVo> voList = new ArrayList<>();


        for (int i = 0; i < positionList.size(); i++) {
            Point_positionVo point_position = positionList.get(i);
            String id = point_position.getId();

            Point_positionVo point_positionVo = new Point_positionVo();

            List<String> viceEchoPathlist = imagesMapper.findViceEchoPathByPid(id);

            List<String> primaryEchoPathlist = imagesMapper.findprimaryEchoPathByPid(id);


            point_positionVo.setId(point_position.getId());
            point_positionVo.setDid(id);
            point_positionVo.setData(point_position.getData());
            point_positionVo.setLng(point_position.getLng());
            point_positionVo.setLat(point_position.getLat());
            point_positionVo.setPrimaryEchoPathlist(primaryEchoPathlist);
            point_positionVo.setViceEchoPathlist(viceEchoPathlist);


            voList.add(point_positionVo);
        }


        return voList;
    }

    @Override
    public void deleteByDid(String did) {

        List<Point_position> point_positionList = point_positiontableMapper.getByDid(did);

        for (int i = 0; i < point_positionList.size(); i++) {

            point_positiontableMapper.deleteByDid(did);
        }
    }

    @Override
    public boolean updatePointModification(String id, String lng, String lat) {

        point_positiontableMapper.updatePointModification(id, lng, lat);

        return true;
    }

    @Override
    public IPage<Point_position> selectPage(Page<Point_position> page, Point_position point_position) {
        //显示分页查询列表
        QueryWrapper<Point_position> queryWrapper = new QueryWrapper<>();

        //2.分页查询
        if (point_position == null) { //没有查询条件 直接返回分页
            return baseMapper.selectPage(page, queryWrapper);
        }

        //3.条件查询
        if (!StringUtils.isEmpty(point_position.getData())) { //data不为空
            queryWrapper.like("data", point_position.getData());
        }


        Page<Point_position> point_positionPage = baseMapper.selectPage(page, queryWrapper);

        return point_positionPage;
    }

    @Override
    public List<Point_position> queryByData(String data) {
        return point_positiontableMapper.queryByData(data);
    }

}
